package com.traffic.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;

import com.traffic.entity.Feedtype;
import com.traffic.entity.UserInfo;
import com.traffic.exception.BaseException;
import com.traffic.model.ApiResponse;
import com.traffic.service.FeedtypeService;
import com.traffic.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.traffic.entity.Feedback;
import com.traffic.service.FeedbackService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.traffic.entity.RFeedback;



@Api(tags = "反馈相关接口")
@RestController
@RequestMapping(value = "/feedbacks")
@Slf4j
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private FeedtypeService feedtypeService;


    @ApiOperation(value = "【反馈】", notes = "反馈列表")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "page", value = "页面", required = true)
    @GetMapping("/all/{page}")
    public ApiResponse AllFeedback(@PathVariable Integer page) {

        Integer count = feedbackService.count();

        IPage<Feedback> feedbackIPage = new Page<Feedback>(page, 10,count);
      //  IPage<Feedback> feedbacks = feedbackService.page(feedbackIPage);
        //时间倒序列表查询

        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        IPage<Feedback> feedbacks = feedbackService.page(feedbackIPage,wrapper.orderByDesc("create_time"));


        List<RFeedback> rFeedbacks = new ArrayList<>();

        for ( Feedback f :feedbacks.getRecords()){
            RFeedback rFeedback = new RFeedback();
            UserInfo userInfo = userInfoService.getUserInfoByUserId(new Long(f.getCreateBy()));
            Feedtype feedtype = feedtypeService.getById(f.getType());

            rFeedback.setId(f.getId());
            rFeedback.setType(feedtype.getName());
            rFeedback.setTitle(f.getTitle());
            rFeedback.setContent(f.getContent());
            rFeedback.setState(f.getState());
            rFeedback.setCreateBy(f.getCreateBy());
            rFeedback.setReply(f.getReply());
            rFeedback.setNickName(userInfo.getNickName());
            rFeedback.setPhone(userInfo.getPhone());
            rFeedback.setCreateTime(f.getCreateTime());
            rFeedback.setImg(f.getImg());
            rFeedback.setLastUpdateTime(f.getLastUpdateTime());

            rFeedbacks.add(rFeedback);

        }


        return ApiResponse.ofStatus(Status.OK, rFeedbacks);




    }

    @ApiOperation(value = "【反馈】", notes = "删除反馈")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "deleteId", value = "删除反馈的id", required = true)
    @DeleteMapping(value = "/delete/{deleteId}")
    public ApiResponse deleteFeedback(@PathVariable Long deleteId) {

        feedbackService.removeById(deleteId);

        return ApiResponse.ofStatus(Status.OK);
    }

    @ApiOperation(value = "【反馈】", notes = "填写回复")
    @PostMapping("/reply")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =Integer.class,key = "id",example = "1" , description = "反馈信息id"),
            @ApiJsonProperty(type =String.class,key = "reply",example = "该问题已解决" , description = "反馈信息"),
            @ApiJsonProperty(type =Integer.class,key = "admin_id",example = "2" , description = "提问用户id"),
        })
    public ApiResponse ReplyFeedback(@RequestBody Map<String, String> param) {

       UpdateWrapper<Feedback> wrapper = new UpdateWrapper<>();

       feedbackService.update(wrapper.set("reply",param.get("reply")).set("state",1).set("admin_id",param.get("admin_id")).eq("id",param.get("id")));

       return ApiResponse.ofStatus(Status.OK);

    }



    @ApiOperation(value = "【搜索反馈】", notes = "关键词模糊查询+日期查询")
    @PostMapping("/search")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =Integer.class,key = "type",example = "2" , description = "反馈类型"),
            @ApiJsonProperty(type =String.class,key = "startdate",example = "2020-10-10" , description = "开始日期"),
            @ApiJsonProperty(type =String.class,key = "enddate",example = "2020-10-21" , description = "结束日期"),
            @ApiJsonProperty(type =String.class,key = "namemobile",example = "13333333" , description = "昵称或者手机号"),
    })
    public ApiResponse searchFeedback(@RequestBody Map<String, String> param) {

        String namo = param.get("namemobile");
        boolean flag = true;

        Integer Ftype = new Integer(param.get("type"));
        List<Feedback> feedbacks = feedbackService.searchByKeywordsAndTime(Ftype, param.get("startdate"), param.get("enddate"));
        List<RFeedback> rFeedbacks = new ArrayList<>();

        for ( Feedback f :feedbacks){
            RFeedback rFeedback = new RFeedback();
            UserInfo userInfo = userInfoService.getUserInfoByUserId(new Long(f.getCreateBy()));

            System.out.println(namo);
            System.out.println(userInfo.getNickName());
            System.out.println(userInfo.getPhone());
            System.out.println(userInfo.getNickName() == namo);
            System.out.println(userInfo.getPhone() ==namo);
            if(userInfo.getNickName().contains(namo) || userInfo.getPhone().contains(namo) || namo==""){

            Feedtype feedtype = feedtypeService.getById(f.getType());

            rFeedback.setId(f.getId());
            rFeedback.setType(feedtype.getName());
            rFeedback.setTitle(f.getTitle());
            rFeedback.setContent(f.getContent());
            rFeedback.setState(f.getState());
            rFeedback.setCreateBy(f.getCreateBy());
            rFeedback.setReply(f.getReply());
            rFeedback.setNickName(userInfo.getNickName());
            rFeedback.setPhone(userInfo.getPhone());
            rFeedback.setCreateTime(f.getCreateTime());
            rFeedback.setImg(f.getImg());
            rFeedback.setLastUpdateTime(f.getLastUpdateTime());

            rFeedbacks.add(rFeedback);
            }

        }

        if (feedbacks.isEmpty()) {
            return ApiResponse.ofStatus(Status.OK);
        }else {
            return ApiResponse.ofStatus(Status.OK, rFeedbacks);
        }

    }

    @ApiOperation(value = "【反馈】", notes = "新增反馈")
    @PostMapping("/insert")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =Integer.class,key = "type",example = "2" , description = "反馈类型"),
            @ApiJsonProperty(type =String.class,key = "title",example = "和平大道发生严重事故" , description = "反馈类型"),

            @ApiJsonProperty(type =String.class,key = "content",example = "我使用xxxx出现了问题" , description = "描述"),

            @ApiJsonProperty(type =String.class,key = "img",example = "www.xxx.com.aaaa|www.xxx.com.bbbb|www.xxx.com.cccc" , description = "图片地址"),
            @ApiJsonProperty(type =Integer.class,key = "createBy",example = "3" , description = "反馈的用户的id"),

    })
    public ApiResponse insertFeedback(@RequestBody Map<String, Object> feedback) {

        String param[] = new String[]{"type","content","createBy","title","img"};

        List<String> paramList = CollUtil.newArrayList(param);
        if (!paramList.containsAll(CollUtil.newArrayList(feedback.keySet().toArray()))) {
            throw new BaseException(Status.PARAM_ERROR);
        }

        Feedback fed = new Feedback();
        BeanUtil.copyProperties(feedback, fed, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        fed.setState(0);
        feedbackService.save(fed);

        return ApiResponse.ofStatus(Status.OK);
    }



    @ApiOperation(value = "【反馈】", notes = "个人反馈列表")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "userid", value = "用户id", required = true)
    @GetMapping("/myfeedback/{userid}")
    public ApiResponse MyFeedback(@PathVariable Long userid) {


        //  IPage<Feedback> feedbacks = feedbackService.page(feedbackIPage);
        //时间倒序列表查询

        QueryWrapper<Feedback> wrapper = new QueryWrapper<>();
        List<Feedback> myfeedbacks = feedbackService.list(wrapper.eq("create_by",userid).orderByDesc("create_time"));


        return ApiResponse.ofStatus(Status.OK, myfeedbacks);


    }



}
